package bank.server.soap;

import java.io.IOException;

import javax.jws.WebService;

import bank.InactiveException;
import bank.OverdrawException;
import bank.driver.local.LocalAccount;
import bank.driver.local.LocalBank;

@WebService (endpointInterface = "bank.server.soap.BankService")
public class BankServiceImpl implements BankService {

	private LocalBank bank;

	public BankServiceImpl() {
		this.bank = new LocalBank();
	}

	@Override
	public String createAccount(String owner) {
		try {
			return this.bank.createAccount(owner);
		} catch (IOException e) {
			return null;
		}
	}

	@Override
	public void deposit(String number, double amount) throws IllegalArgumentException, InactiveException, IOException {
		LocalAccount account = this.bank.getAccount(number);
		account.deposit(amount);
	}

	@Override
	public Object[] getAccountNumbers() {
		try {
			return this.bank.getAccountNumbers().toArray();
		} catch (IOException e) {
			return null;
		}
	}

	@Override
	public double getBalance(String number) throws IOException {
		LocalAccount account = this.bank.getAccount(number);
		return account.getBalance();
	}

	@Override
	public String getOwner(String number) throws IOException {
		LocalAccount account = this.bank.getAccount(number);
		return account.getOwner();
	}

	@Override
	public boolean isActive(String number) throws IOException {
		LocalAccount account = this.bank.getAccount(number);
		return account.isActive();
	}

	@Override
	public boolean removeAccount(String number) {
		try {
			return this.bank.removeAccount(number);
		} catch (IOException e) {
			return false;
		}
	}

	@Override
	public void transfer(String numberA, String numberB, double amount) throws IllegalArgumentException, OverdrawException, InactiveException, IOException {
		LocalAccount accountA = this.bank.getAccount(numberA);
		LocalAccount accountB = this.bank.getAccount(numberB);

		this.bank.transfer(accountA, accountB, amount);
	}

	@Override
	public void withdraw(String number, double amount) throws IllegalArgumentException, OverdrawException, InactiveException, IOException {
		LocalAccount account = this.bank.getAccount(number);
		account.withdraw(amount);
	}
}
